import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SceneServlet extends HttpServlet {

    private String name = "C:/pruebaServlet.txt";
    private int opcion;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException
            {
        try {

            
            /* OutputStream outputStream = response.getOutputStream();
            ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(outputStream);

            objectOutputStream1.writeObject(true);
            objectOutputStream1.flush();
            objectOutputStream1.close();

*/

            
            InputStream inputStream = request.getInputStream();
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            String o = (String) objectInputStream.readObject();
            //Object s = (Object) objectInputStream.readObject();
            objectInputStream.close();
            
            File file = new File("C:/e1.txt");
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(o);
            objectOutputStream.close();
            
            OutputStream outputStream = response.getOutputStream();
            ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(outputStream);

            objectOutputStream1.writeObject(true);
            objectOutputStream1.flush();
            objectOutputStream1.close();
            
           /* if (o instanceof String) {
                String operacion = (String) o;
                if (operacion.equals("save")) {
                    opcion = 1;

                    OutputStream outputStream = response.getOutputStream();
                    ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(outputStream);

                    objectOutputStream1.writeBoolean(true);
                    objectOutputStream1.flush();
                    objectOutputStream1.close();
                } else if (operacion.equals("load")) {
                    opcion = 2;

                    OutputStream outputStream = response.getOutputStream();
                    ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(outputStream);

                    objectOutputStream1.writeBoolean(true);
                    objectOutputStream1.flush();
                    objectOutputStream1.close();
                }
            } else {
                ISceneSaver sceneSaver = new FileSceneSaver();
                if (opcion == 1) {
                    sceneSaver.write(name, o);
                } else if (opcion == 2) {
                    sceneSaver.load(name);
                }
            }*/
        } catch (Exception ex) {
            File file = new File("C:/error1.txt");
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(ex.toString());
            objectOutputStream.close();
            Logger.getLogger(SceneServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
